package com.bee.base.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.bee.base.util.BaseConstants;

@SuppressWarnings("rawtypes")
public class Page implements Serializable {

	private static final long serialVersionUID = 6063593133411723364L;

	private List result;
	private int startIndex;
	private int pageSize;
	private int totalCount;

	public Page() {
		this(new ArrayList(), 0, BaseConstants.PAGE_SIZE, 0);
	}

	@SuppressWarnings("unchecked")
	public Page(List results, int startIndex, int pageSize, int totalCount) {
		if (results == null) {
			this.result = new ArrayList();
		} else if (results.size() <= pageSize) {
			this.result = results;
		} else {
			this.result = new ArrayList();
			for (int i = startIndex; (i < startIndex + pageSize) && (i < results.size()); i++)
				this.result.add(results.get(i));
		}
		this.startIndex = startIndex;
		this.pageSize = pageSize;
		this.totalCount = totalCount;
	}

	public List getResult() {
		return result;
	}

	public int getCurrentPageNo() {
		return startIndex / pageSize + 1;
	}

	public int getLastPageNo() {
		return (totalCount + pageSize - 1) / pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public boolean hasPrevPage() {
		return getCurrentPageNo() > 1;
	}

	public boolean hasNextPage() {
		return getCurrentPageNo() < getLastPageNo();
	}

	public static int getStartOfPage(int pageNo, int pageSize) {
		return Math.max((pageNo - 1) * pageSize, 0);
	}

	public String toString() {
		return new ToStringBuilder(this).append("totalCount", this.getTotalCount())
				.append("currentPageNo", this.getCurrentPageNo()).append("lastPageNo", this.getLastPageNo())
				.append("result", this.getResult()).toString();
	}

}
